package RMI;

import java.net.InetAddress;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import Client.ClientDetails;
import Client.Manager;
import Client.ServerToClient;
import Messages.CreateFolderMsg;
import Messages.Msg;
import Messages.ShareMsg;
import TagsDB.Plugin;
import TagsDB.TagedFile;
import Watcher.watchService;


public class RmiService extends UnicastRemoteObject implements ReceiveMessageInterface
{


 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 int thisPort;
 String thisAddress;
 Registry registry;
 int folderId;
 private Manager manager =Manager.getInstance();


 public RmiService() throws RemoteException
 {
  try
  {
   thisAddress=(InetAddress.getLocalHost()).toString();
  }
  catch (Exception e) {
   throw new RemoteException("Can't get inet address");
  }
  thisPort=1099;
  try
  {
   registry=LocateRegistry.createRegistry(thisPort);
   registry.rebind("serviceRMI",this);
   System.out.println("create a port "+thisPort+" for RMI");
  }
  catch (RemoteException e) {
   throw e;
  }


 }
 @Override
 public ArrayList<TagedFile> search(String fileName,
   ArrayList<String> options) {
  return ManagerAppGUI.getManagerAppGUIinstance().search(fileName, options);
 }
 @Override
 public void restorePlugin() {
  ManagerAppGUI.getManagerAppGUIinstance().restorePlugin();
 }
 @Override
 public boolean checkPluginExist(String pluginName) {
  return ManagerAppGUI.getManagerAppGUIinstance().checkPluginExist(pluginName);
 }
 @Override
 public void updateUsePlugin(String pluginName, boolean check) {
  ManagerAppGUI.getManagerAppGUIinstance().updateUsePlugin(pluginName, check);
 }
 @Override
 public void addPluginToDB(String strPath, String strNamePlugin) {
  ManagerAppGUI.getManagerAppGUIinstance().addPluginToDB(strPath, strNamePlugin);

 }
 @Override
 public ArrayList<Plugin> getPlugins() {
  // TODO Auto-generated method stub
  return ManagerAppGUI.getManagerAppGUIinstance().getPlugins();
 }
 @Override
 public ArrayList<String> allOptionTags() {
  // TODO Auto-generated method stub
  System.out.println("guy14");
  return ManagerAppGUI.getManagerAppGUIinstance().allOptionTags();
 }
 public boolean playService()
 {
  boolean play=watchService.getWsInstance().isPlay();
  if(play)
  {
   return false;
  }
  else
  {
   ServerToClient.getInstance().setPlay(true);
   watchService.getWsInstance().setPlay(true);
   return true;
  }
 }
 public boolean pauseService()
 {
  boolean play=watchService.getWsInstance().isPlay();
  if(!play)
  {
   return false;
  }
  else
  {
   ServerToClient.getInstance().setPlay(false);
   watchService.getWsInstance().setPlay(false);
   return true;
  }
 }
 @Override
 public ArrayList<String> optionsForSearch(boolean check, String itemToSearch) {
  return ManagerAppGUI.getManagerAppGUIinstance().optionsForSearch(check, itemToSearch);
 }
 @Override
 public ArrayList<String> getOptionsToSearch() {
  return ManagerAppGUI.getManagerAppGUIinstance().getOptionsToSearch();
 }
 @Override
 public void takeUserToShare(String userToShare, String folderName)
 throws RemoteException 
 {
  System.out.println("IN RMI GOING TO SEND CREATE FOLDER");
  Msg msg=new CreateFolderMsg(folderName);
  manager.addMsg(msg);
  System.out.println("AFTER CREATE");
  ArrayList<String> users=new ArrayList<String>();
  boolean flag=true;
  int beginIndex=0,endIndex=0;
  String strCopyUsers=userToShare;

  if(userToShare.isEmpty())
  {
   return;
  }


  while(flag)
  {
   endIndex=strCopyUsers.indexOf(",", beginIndex);
   if(endIndex==-1)
   {
    endIndex=strCopyUsers.length();
    flag=false;
   }
   String strUser=strCopyUsers.substring(beginIndex, endIndex);
   if(!strUser.isEmpty())
   {
    users.add(strUser);
   }
   beginIndex=endIndex+1;

  }
  for(int i=0; i<users.size(); i++)
  {
   System.out.println(users.get(i).toString());
  }
  System.out.println("SENDING SHARE MESSAGE");
  msg=new ShareMsg(users, folderName);
  manager.addMsg(msg);
  //return users;
 }
 @Override
 public void clearOptionsToSearch() throws RemoteException {
  // TODO Auto-generated method stub
  ManagerAppGUI.getManagerAppGUIinstance().clearOptionsToSearch();
 }
 @Override
 public String getFolderPath() throws RemoteException {
  // TODO Auto-generated method stub
  ClientDetails cd;
  try {
   cd = XML.ConfigHelper.readXmlClient();
   return cd.getMainFolder();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
 }




}